﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.AcmsControls.ContentControls.Admin;

namespace Acms.AcmsControls.ContentControls.Admin
{
    [ToolboxData("<{0}:EditControlLoader runat=server></{0}:EditControlLoader>")]
    public class AdminContentControlLoader : CompositeControl, ISpellCheck
    {
        public AdminContentControlLoader()
        {
            Mode = Modes.Display;
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (Mode == Modes.Display)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "adminDisplayContent");
            }
            else if(Mode == Modes.Edit)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "adminEditContent");
            }
            base.AddAttributesToRender(writer);
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        } 

        public IBaseAdminDisplay Control
        {
            get
            {
                if (this.Controls.Count > 0)
                {
                    return this.Controls[0] as IBaseAdminDisplay;
                }
                else
                {
                    return null;
                }
            }
        }

        protected override void CreateChildControls()
        {
            if (!string.IsNullOrEmpty(ControlType))
            {
                var ctrl = Page.LoadControl(System.Type.GetType(ControlType, true, true), null);
                if (ctrl != null)
                {
                    this.Controls.Add(ctrl);
                }
            }
            base.CreateChildControls();
        }

        public override void DataBind()
        {
            if (PostControl != null)
            {
                if (Post == null)
                {
                    Post = Acms.Bll.Post.GetById(SiteId, PostControl.PostId, SiteVersion);
                }
                if (Mode == Modes.Display)
                {
                    ControlType = PostControl.AdminDisplayControlType;
                }
                else
                {
                    ControlType = PostControl.AdminEditControlType;
                }
                this.Controls.Clear();
                this.CreateChildControls();
                EnsureChildControls();
                if (Control != null)
                {
                    Control.LoadData(PostControl, Post);
                    Control.DataBind();
                }
            }
        }

        public string ControlType { get; private set; }
        public int SiteId { get; set; }
        public DateTime? SiteVersion { get; set; }

        public Acms.Bll.PostControl PostControl { get; set; }
        public Acms.Bll.Post Post { get; set; }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            ControlType = ViewState["ControlType"] as string;
            SiteVersion = ViewState["SiteVersion"] as DateTime?;
            SiteId = (int)ViewState["SiteId"];
        }

        protected override object SaveViewState()
        {
            ViewState["ControlType"] = ControlType;
            ViewState["SiteVersion"] = SiteVersion;
            ViewState["SiteId"] = SiteId;
            return base.SaveViewState();
        }

        public enum Modes
        {
            Display,
            Edit
        }

        public Modes Mode { get; set; }

        #region ISpellCheck Members

        public bool PerformSpellCheck
        {
            get 
            {
                EnsureChildControls();
                if (this.Controls[0] is ISpellCheck)
                {
                    return ((ISpellCheck)this.Controls[0]).PerformSpellCheck;
                }
                else
                {
                    return false;
                }
            }
        }

        public string ClientSpellTextSource
        {
            get
            {
                EnsureChildControls();
                if (this.Controls[0] is ISpellCheck)
                {
                    return ((ISpellCheck)this.Controls[0]).ClientSpellTextSource;
                }
                else
                {
                    return null;
                }
            }
        }

        public string[] ControlsToSpellCheck
        {
            get
            {
                EnsureChildControls();
                if (this.Controls[0] is ISpellCheck)
                {
                    return ((ISpellCheck)this.Controls[0]).ControlsToSpellCheck;
                }
                else
                {
                    return null;
                }
            }
        }

        #endregion
    }
}
